<template>
  <div>
    <a-tabs :activeKey="first_tab" @change="activeKeyChange">
       <a-tab-pane key="1" tab="在租">
        <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索條件">
                    <a-select  style="width: 100%" v-model="queryParam.searchType">
                      <a-select-option  style="width: 100%" :value="0">全部</a-select-option>
                      <a-select-option  style="width: 100%" :value="1">房產名</a-select-option>
                      <a-select-option  style="width: 100%" :value="2">房間名</a-select-option>
                      <a-select-option  style="width: 100%" :value="3">租客姓名</a-select-option>
                      <a-select-option  style="width: 100%" :value="4">租客手機號</a-select-option>
                      <a-select-option  style="width: 100%" :value="5">租客身份證號</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                 <a-col :md="6" :sm="24">
                  <a-form-item label="">
                    <a-input v-model="queryParam.searchValue" allow-clear placeholder="请输入搜索值"/>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24" >
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                    <a-button style="margin-left: 8px" @click="() => queryParam = { type: 1, numType: 0 }">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
        <a-card :bordered="false">
          <s-table
            ref="table"
            size="default"
            :columns="columns"
            :data="loadData"
            :alert="true"
            :rowKey="(record) => record.id">
            <template class="table-operator" slot="operator" >
              <a-row>
                <a-col :span="12">
                  <a-button @click="queryByNumType(0)" :class="queryParam.numType ===0 ? 'numTypeActive' : ''" type="dashed" size="small">全部({{ numData.alls }})</a-button>
                  <a-button @click="queryByNumType(1)" :class="queryParam.numType ===1 ? 'numTypeActive' : ''" type="dashed" size="small">七日内到期({{ numData.dqrn }})</a-button>
                  <a-button @click="queryByNumType(2)" :class="queryParam.numType ===2 ? 'numTypeActive' : ''" type="dashed" size="small">已到期({{ numData.ydq }})</a-button>
                </a-col>
                <a-col :span="12">
                  <a-button class="bt-right" type="primary" @click="$refs.addForm.add(1)">添加租約</a-button>
                  <a-button class="bt-right" type="primary" @click="exportExcelByType(1)">導出EXCEL</a-button>
                </a-col>
              </a-row>
            </template>
            <span slot="action" slot-scope="text, record">
                      <a @click="$refs.leaseDetail.edit(record.id)">詳情</a>
                      <a-divider type="vertical" />
                      <a-dropdown >
                        <a class="ant-dropdown-link">
                          更多 <a-icon type="down" />
                        </a>
                        <a-menu slot="overlay">
                          <a-menu-item>
                            <a @click="lookBillById(record.id)">查看賬單</a>
                          </a-menu-item>
                          <a-menu-item v-if="record.leaseStatus === 1" >
                            <a @click="$refs.houseBillPlanForm.edit(record)" >租金調整</a>
                          </a-menu-item>
                          <a-menu-item  v-if="record.leaseStatus === 1">
                            <a @click="$refs.tuiForm.edit(record.id)">退租</a>
                          </a-menu-item>
                          <a-menu-item  v-if="record.leaseStatus !== 2">
                            <a @click="$refs.xuzuForm.edit(record.id)">續租</a>
                          </a-menu-item>
                          <!-- <a-menu-item>
                            <a @click="$refs.cancelRoomReservation.edit(record.id)">換房</a>
                          </a-menu-item> -->
                          <a-menu-item>
                            <a @click="$refs.printLease.edit(record.id)">打印</a>
                          </a-menu-item>
                        </a-menu>
                      </a-dropdown>
            </span>
          </s-table>
        </a-card>
       </a-tab-pane>
       <a-tab-pane key="2" tab="歷史租約">
          <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                 <a-col :md="6" :sm="24">
                  <a-form-item label="搜索條件">
                    <a-select  style="width: 100%" v-model="queryParam2.searchType">
                      <a-select-option  style="width: 100%" :value="0">全部</a-select-option>
                      <a-select-option  style="width: 100%" :value="1">房產名</a-select-option>
                      <a-select-option  style="width: 100%" :value="2">房間名</a-select-option>
                      <a-select-option  style="width: 100%" :value="3">租客姓名</a-select-option>
                      <a-select-option  style="width: 100%" :value="4">租客手機號</a-select-option>
                      <a-select-option  style="width: 100%" :value="5">租客身份證號</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="">
                    <a-input v-model="queryParam2.searchValue" allow-clear placeholder="请输入搜索值"/>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24" >
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table2.refresh(true)" >查询</a-button>
                    <a-button style="margin-left: 8px" @click="() => queryParam2 = { type: 2 }">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
        <a-card :bordered="false">
          <s-table
            ref="table2"
            size="default"
            :columns="columns"
            :data="loadData2"
            :alert="true"
            :rowKey="(record) => record.id">
            <template class="table-operator" slot="operator">
              <a-button type="primary" @click="exportExcelByType(2)">導出EXCEL</a-button>
            </template>
            <span slot="action" slot-scope="text, record">
                      <a @click="$refs.leaseDetail.edit(record.id)">詳情</a>
                      <a-divider type="vertical" />
                      <a-dropdown >
                        <a class="ant-dropdown-link">
                          更多 <a-icon type="down" />
                        </a>
                        <a-menu slot="overlay">
                          <a-menu-item>
                            <a @click="lookBillById(record.id)">查看賬單</a>
                          </a-menu-item>
                          <a-menu-item>
                            <a @click="$refs.cancelRoomReservation.edit(record.id)">刪除</a>
                          </a-menu-item>
                        </a-menu>
                      </a-dropdown>
                    </span>
          </s-table>
        </a-card>
       </a-tab-pane>
    </a-tabs>
    <add-form ref="addForm" @ok="handleOk" />
    <edit-form ref="editForm" @ok="handleOk" />
    <print-lease ref="printLease"/>
    <lease-detail ref="leaseDetail"/>
    <house-bill-plan-form ref="houseBillPlanForm" @ok="handleOk" />
    <xuzu-form ref="xuzuForm" @ok="handleOk" />
    <tui-form ref="tuiForm" @ok="handleOk" />
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { houseLeasePage, houseLeaseDelete, getTjSys, exportExcel } from '@/api/modular/main/houselease/houseLeaseManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import printLease from './printLease.vue'
  import leaseDetail from './leaseDetail.vue'
  import houseBillPlanForm from '../housebill/houseBillPlanForm.vue'
  import xuzuForm from '../housebill/xuzuForm.vue'
  import tuiForm from '../housebill/tuiForm.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      printLease,
      leaseDetail,
      houseBillPlanForm,
      xuzuForm,
      tuiForm
    },
    data () {
      return {
        first_tab: '1',
        // 高级搜索 展开/关闭
        advanced: false,
         // 查询参数
        queryParam: {
          type: 1,
          numType: 0
        },
        queryParam2: {
          type: 2
        },
        // 表头
        columns: [
          {
            title: '入住房間',
            align: 'center',
            dataIndex: 'roomName'
          },
          {
            title: '承租人',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '聯係方式',
            align: 'center',
            dataIndex: 'phone'
          },
          {
            title: '身份證號',
            align: 'center',
            dataIndex: 'idCard'
          },
          {
            title: '租金',
            align: 'center',
            dataIndex: 'rent'
          },
          {
            title: '押金',
            align: 'center',
            dataIndex: 'deposit'
          },
          {
            title: '收租周期',
            align: 'center',
            dataIndex: 'receiptCycle'
          },
          {
            title: '起租日期',
            align: 'center',
            dataIndex: 'rentStartDate'
          },
          {
            title: '到租日期',
            align: 'center',
            dataIndex: 'rentEndDate'
          },
          {
            title: '备注',
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '操作',
            width: '150px',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          this.initNeedData()
          return houseLeasePage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        loadData2: parameter => {
          return houseLeasePage(Object.assign(parameter, this.queryParam2)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        numData: {}
      }
    },
    created () {
    },
    methods: {
       // 第一步tab点击
      activeKeyChange(key) {
        this.first_tab = key
      },
      initNeedData() {
        getTjSys().then((res) => {
          if (res.code === 200) {
            this.numData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      houseLeaseDelete (record) {
        houseLeaseDelete(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      lookBillById (id) {
          this.$router.push({ path: '/housebilldetailbyid', query: { id: id } })
      },
      queryByNumType(type) {
        this.queryParam.numType = type
        this.$refs.table.refresh(true)
      },
      exportExcelByType(type) {
        if (type === 1) {
          exportExcel(this.queryParam).then((res) => {
             this.downloadfile(res)
          })
        } else {
          exportExcel(this.queryParam2).then((res) => {
            this.downloadfile(res)
          })
        }
      },
      downloadfile (res) {
        var blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' })
        var contentDisposition = res.headers['content-disposition']
        var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var result = patt.exec(contentDisposition)
        var filename = result[1]
        var downloadElement = document.createElement('a')
        var href = window.URL.createObjectURL(blob) // 创建下载的链接
        var reg = /^["](.*)["]$/g
        downloadElement.style.display = 'none'
        downloadElement.href = href
        downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href)
      }
    }
  }
</script>
<style lang="less" scoped>
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .numTypeActive{
    color: #40a9ff;
    background-color: #fff;
    border-color: #40a9ff;
  }
  .bt-right{
    float:right;
  }
</style>
